(function() {
    'use strict';

    angular
        .module('milkbeeApp')
        .controller('AdvertisementDialogController', AdvertisementDialogController);

    AdvertisementDialogController.$inject = ['$timeout', '$scope','$http', '$stateParams', '$uibModalInstance', 'entity', 'Advertisement'];

    function AdvertisementDialogController ($timeout, $scope,$http, $stateParams, $uibModalInstance, entity, Advertisement) {
        var vm = this;
        vm.datepickerOptions= {
            	minDate:new Date(),
            	maxDate: null
        }
        vm.advertisement = entity;
        vm.clear = clear;
        vm.datePickerOpenStatus = {};
        vm.openCalendar = openCalendar;
        vm.save = save;
        vm.categories={};
        $timeout(function (){
            angular.element('.form-group:eq(1)>input').focus();
        });

        function clear () {
            $uibModalInstance.dismiss('cancel');
        }

        function save () {
            vm.isSaving = true;
            if (vm.advertisement.id !== null) {
                Advertisement.update(vm.advertisement, onSaveSuccess, onSaveError);
            } else {
                Advertisement.save(vm.advertisement, onSaveSuccess, onSaveError);
            }
        }

        function onSaveSuccess (result) {
            $scope.$emit('milkbeeApp:advertisementUpdate', result);
            $uibModalInstance.close(result);
            vm.isSaving = false;
        }

        function onSaveError () {
            vm.isSaving = false;
        }

        vm.datePickerOpenStatus.createdAt = false;
        vm.datePickerOpenStatus.startAt = false;
        vm.datePickerOpenStatus.endAt = false;

        function openCalendar (date) {
            vm.datePickerOpenStatus[date] = true;
        }
        
        
        function loadCategories(){
        	$http({
        		method: 'get',
        		url: '/api/categoriesbylevel/1',
        		transformRequest: angular.identity
        	}).success(function(ret){
        		vm.categories=ret;
        	});
        }
       
        vm.onCategoryChange=function(){
        	vm.advertisement.categoryId=vm.category.id;
        }  
        vm.onImageSelect=function($files){
        	vm.doUpload=true;
        	for (var i = 0; i < $files.length; i++) {        		
        		var fd = new FormData();
        		//Take the first selected file
        		fd.append("jyfile", $files[i]);     	      
        		$http({
        			method: 'post',
        			url: '/upload/advertisement',
        			data:fd,
        			headers: {'Content-Type': undefined},
        			transformRequest: angular.identity,
        			uploadEventHandlers: {
    	                progress: function(e) {
    	                	if (e.lengthComputable) {
    	                		vm.currentProcess = Math.round(e.loaded * 100 / (e.total*1.1));
    	                        if (e.loaded == e.total) {
    	                        }
    	                    }
    	                }
    	            }
        		}).success(function(ret) {
        			if (ret.success) {
        				vm.currentProcess=100;
        				vm.advertisement.image=ret.path;
        				
        			}else if(!ret.success){
        				vm.currentProcess=0;
        			}
        		})
        		//only one file
        		break;
        	}
        }
        
        loadCategories();
    }
})();
